package cn.wawi.controller.sys;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import cn.wawi.common.easyui.DataGrid;
import cn.wawi.common.easyui.Tree;
import cn.wawi.controller.BaseController;
import cn.wawi.entity.sys.Department;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value="/sys_department")
public class DepartmentController extends BaseController<Department>{
	
	@InitBinder({"parent"})
	public void initBinder1(WebDataBinder binder) {
		binder.setFieldDefaultPrefix("parent.");
	}
	
	@ResponseBody
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public DataGrid list(HttpServletRequest request){
		return findAll(request);
	}
	/**
	 * 获取所有部门树
	 * @return easyui-tree
	 */
	@ResponseBody
	@RequestMapping(value="/tree",method=RequestMethod.GET)
	public List<Tree> tree(){
		List<Department> list = departmentDao.findTree();
		return trees(list);
	}
	public List<Tree> trees(List<Department> list){
		List<Tree> trees=new ArrayList<Tree>();
		for(Department d: list){
			Tree tree=new Tree(d.getId(), d.getName());
			tree.setChildren(trees(d.getChildren()));
			trees.add(tree);
		}
		return trees;
	}
}
